<?php

namespace App\Model;

use Carbon\Carbon;

class LoginLogsModel extends BaseModel{

    protected $table = 'login_logs';

    const STATUS_0 = 0;//失败
    const STATUS_1 = 1;//成功
    const STATUS_TEXT = [
        self::STATUS_0 => '<span class="el-tag el-tag--danger">失败</span>',
        self::STATUS_1 => '<span class="el-tag">成功</span>',
    ];

    protected $fillable = [
        'id','password','status','username','login_ip','login_at'
    ];

    /**
     * 生成日志记录
     * @param $data 登录数据
     * @param $ip 客户端IP
     * @param int $type 类型 0非法登录1成功登录
     * @return bool|int
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function createLog($data,$ip,$type = 1){
        $insert = [
            'username' => $data['username'],
            'password' => $type ? '******' : $data['password'],
            'status'   => $type ? self::STATUS_1 : self::STATUS_0,
            'login_ip' => $ip,
            'login_at' => Carbon::now(),
        ];
        return $this->create($insert);
    }




}
